from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By
from basepage.base import Base

class LoginPage(Base):
    username = (By.XPATH, '//*[@id="account"]')
    password = (By.NAME, 'password')
    loginBtn = (By.ID, 'submit')
    keepLogin = (By.XPATH, '//*[@id="keeplogin"]/div')
    toubiao = (By.ID, 'main-avatar')
    logout = (By.LINK_TEXT, '退出')

    # 默认进行登录功能
    def login_stream(self, name, pwd):
        self.find_element(*self.username).send_keys(name)
        self.find_element(*self.password).send_keys(pwd)
        self.find_element(*self.loginBtn).click()

    # 保持登录流程
    def login_keep_stream(self, name, pwd):
        self.find_element(*self.username).send_keys(name)
        self.find_element(*self.password).send_keys(pwd)
        self.find_element(*self.keepLogin).click()
        self.find_element(*self.loginBtn).click()

    # 退出流程
    def logout_stream(self):
        self.find_element(By.ID, 'appIframe-my')
        self.driver.switch_to.frame('appIframe-my')
        self.find_element(*self.toubiao).click()
        self.find_element(*self.logout).click()


if __name__ == '__main__':
    url = 'http://127.0.0.1:2222/zentao/user-login.html'
    try:
        driver = webdriver.Chrome()
        driver.maximize_window()
        case = LoginPage(driver)
        driver.get(url)
        case.find_element(*case.keepLogin).click()
        sleep(1)
        case.login_stream('admin', '362326aaA')
        # case.find_element(*(By.LINK_TEXT, '测试')).click()
        print(driver.title)
        case.logout_stream()
        sleep(5)
    finally:
        sleep(1)
        driver.quit()